package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.ReSaleBean;
import org.gangcai.mac.shop.bean.ReadedBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.common.VerificationCodeHelper;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.views.VerticalStepView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class ShopReSaleActivity extends MultiStatusActivity {

    @BindView(R.id.timeTips)
    TextView timeTips;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.protoCv)
    CardView protoCv;
    @BindView(R.id.step_view0)
    VerticalStepView stepView0;
    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.goodImage)
    ImageView goodImage;
    @BindView(R.id.goodTitle)
    TextView goodTitle;
    @BindView(R.id.goodMoney)
    TextView goodMoney;
    @BindView(R.id.yiyuqi)
    TextView yiyuqi;
    @BindView(R.id.backMoneyTv)
    TextView backMoneyTv;
    @BindView(R.id.yanzhengmaEt)
    EditText yanzhengmaEt;
    @BindView(R.id.getVerificationCode)
    TextView getVerificationCode;
    @BindView(R.id.yanzhengmaCv)
    CardView yanzhengmaCv;
    @BindView(R.id.yanzhengmaEtLl)
    LinearLayout yanzhengmaEtLl;
    private String money;
    private String order_id;
    private String gid;
    private String backMoney;
    private String shopName;
    private String reSaleid;
    private String imageUrl;
    boolean isReSale = false;

    /**
     * 是否阅读了购物协议
     */
    private boolean hasReadProto = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        Bundle extras = getIntent().getExtras();
        money = extras.getString("money");
        order_id = extras.getString("order_id");
        gid = extras.getString("gid");
        shopName = extras.getString("shopName");
        reSaleid = extras.getString("reSaleid");
        imageUrl = extras.getString("imageUrl");
        initTitleBarView(titlebar, "商品转卖");
        if (!TextUtils.isEmpty(reSaleid)) {
            isReSale = true;
            loadData();
            yanzhengmaEtLl.setVisibility(View.GONE);
        } else {
            //没有转买过
            initStepView(4);
            new VerificationCodeHelper(getVerificationCode) {
                @Override
                public void onClick(View v) {
                    final VerificationCodeHelper temp = this;
                    RetrofitManager.getInstance().create(AppAPI.class)
                            .sendCode(SPUtils.getInstance().getString("phoneNumber"), "4")
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CommonObserver<CommonResonseBean>() {
                                @Override
                                public void onSuccess(CommonResonseBean value) {
                                    if (value.getCode() == 101) {
                                        temp.onSuccess();
                                    } else {
                                        temp.onFailure();
                                    }
                                }

                                @Override
                                public boolean onFailure(Throwable e) {
                                    temp.onFailure();
                                    return super.onFailure(e);
                                }
                            });

                }
            };
        }
        bindView();
    }

    private void loadData() {
        multipleStatusView.showLoading();
        RetrofitManager.create().goods_resell_info(reSaleid)
                .compose(this.<ReSaleBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<ReSaleBean>(multipleStatusView) {
                    @Override
                    public void onSuccess(ReSaleBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            LogUtils.w("value" + value);
                            if (!isReSale) {
                                btn.setBackgroundResource(R.mipmap.common_btn_bg);
                                btn.setEnabled(true);
                            } else {
                                btn.setBackgroundResource(R.mipmap.sqytjbtn);
                                btn.setEnabled(false);
                            }
                            initStepView(value.getState());

                        }
                    }
                });

    }

    private void bindView() {
        Glide.with(ShopReSaleActivity.this).load(Constant.IMAGE_PATH + imageUrl).into(goodImage);
        goodMoney.setText("¥" + money);
        goodTitle.setText(shopName);
        time.setVisibility(View.INVISIBLE);
        yiyuqi.setVisibility(View.INVISIBLE);
        timeTips.setText("三方回收价 : ¥" + money);
        timeTips.setTextColor(getResources().getColor(R.color.textColorDark));
        backMoneyTv.setText("¥" + money);
        backMoney = money + "";

    }

    private void initStepView(int state) {
        int step = 0;
        if (state == 1) {
            step = 3;
        } else if (state == 2) {
            step = 2;
        } else if (state == 3) {
            step = 1;
        } else if (state == 4) {
            step = 3;
        }

        List<String> list0 = new ArrayList<>();

        list0.add("待提交审核");
        list0.add("审核中");
        list0.add("已到账");

        stepView0.setStepsViewIndicatorComplectingPosition(step)//设置完成的步数
                .reverseDraw(false)//default is true
                .setStepViewTexts(list0)//总步骤
                .setTextSize(14)
                .setLinePaddingProportion(1)//设置indicator线与线间距的比例系数
                .setStepsViewIndicatorCompletedLineColor(ContextCompat.getColor(this, R.color.colorPrimary_indicator))//设置StepsViewIndicator完成线的颜色
                .setStepsViewIndicatorUnCompletedLineColor(ContextCompat.getColor(this, R.color.soliderColor))//设置StepsViewIndicator未完成线的颜色
                .setStepViewComplectedTextColor(ContextCompat.getColor(this, R.color.colorPrimary_indicator))//设置StepsView text完成线的颜色
                .setStepViewUnComplectedTextColor(ContextCompat.getColor(this, R.color.textColorDark))//设置StepsView text未完成线的颜色
                .setStepsViewIndicatorCompleteIcon(ContextCompat.getDrawable(this, R.mipmap.jindu1))//设置StepsViewIndicator CompleteIcon
                .setStepsViewIndicatorDefaultIcon(ContextCompat.getDrawable(this, R.mipmap.jindu))//设置StepsViewIndicator DefaultIcon
                .setStepsViewIndicatorAttentionIcon(ContextCompat.getDrawable(this, R.mipmap.jindu));//设置StepsViewIndicator AttentionIcon
    }

    @Override
    public int getContentView() {
        return R.layout.activity_shop_re_sale;
    }

    @OnClick({R.id.btn, R.id.protoCv})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn) {
            if (hasReadProto) {
                submitData();
            } else {
                ToastUtils.showShort("请阅读并同意商品转卖协议");
            }

        } else if (view.getId() == R.id.protoCv) {
            BrowerActivity.openURL(RetrofitManager.server_address + "resell_info", "商品转卖协议", true);
        }
    }

    private void submitData() {
        prompDialog.showLoading("请等待");
        RetrofitManager.create().goods_resell(SPUtils.getInstance().getString("uid"),
                gid, order_id, backMoney).compose(this.<CommonResonseBean>bindToLifecycle()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            ToastUtils.showShort("已提交服务器");
                        }
                    }
                });

    }

    @Subscribe
    public void event(ReadedBean bean) {
        hasReadProto = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public static void open(String shopName, String money, String order_id, String gid, String reSaleid, String imageUrl) {

        Bundle extras = new Bundle();
        extras.putString("money", money);
        extras.putString("order_id", order_id);
        extras.putString("gid", gid);
        extras.putString("shopName", shopName);
        extras.putString("reSaleid", reSaleid);
        extras.putString("imageUrl", imageUrl);
        ActivityUtils.startActivity(extras, ShopReSaleActivity.class);
    }
}
